/**
 * Session监听器
 */
package cn.antia.cep.common;

import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import cn.antia.cep.busi.*;
import cn.antia.cep.db.hibernate.bean.*;


/**
 * @author sunkeei
 * @date Mar 7, 2007
 * @version 0.1
 */
public class AntiaHttpSessionListener implements HttpSessionListener {
	/**
	 * 会话创建方法
	 */
	public void sessionCreated(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		ServletContext application = session.getServletContext();
		// 在线人数
		Integer onlineNum = (Integer) application
				.getAttribute(SessionConst.ONLINE);
		onlineNum = onlineNum == null ? 1 : onlineNum;
		application.setAttribute(SessionConst.ONLINE, onlineNum);
		// 日仿问量
		Integer dayVisits = (Integer) application
				.getAttribute(SessionConst.DAY_VISIT);
		if (dayVisits == null) {
			dayVisits = 1;
		} else {
            dayVisits += 1;
		}
		application.setAttribute(SessionConst.DAY_VISIT, dayVisits);
		HashMap usermap = (HashMap) application.getAttribute(SessionConst.USER_MAP);
		if (usermap == null) {
			usermap = new HashMap();
			application.setAttribute(SessionConst.USER_MAP, usermap);
		}
		VisitLog log = new VisitLog();
		log.setSessionId(session.getId());
		log.setSession(session);
		log.setLoginTime(new Date());
		DbHibernateService service = (DbHibernateService)ContextBean.getBean("dbHibernateService");
		service.create(log);
		usermap.put(session.getId(), log);
		AntiaLog.info(new StringBuffer("新建了一个SESSION！SESSION总数为").append(onlineNum).toString());
	}

	/**
	 * SESSION销毁方法
	 */
	public void sessionDestroyed(HttpSessionEvent event) {
		Date destoyedTime = new Date();
		DbHibernateService service = (DbHibernateService) ContextBean.getBean("dbHibernateService");

		HttpSession session = event.getSession();
		String sessionId = session.getId();
		ServletContext application = session.getServletContext();
		HashMap usermap = (HashMap) application.getAttribute(SessionConst.USER_MAP);
		if(usermap != null){
			// 仿问日志
			VisitLog log = (VisitLog) usermap.get(sessionId);
			// 在线人数
			Integer onlineNum = (Integer) application.getAttribute(SessionConst.ONLINE);
			onlineNum = new Integer(onlineNum.intValue() - 1);
			application.setAttribute(SessionConst.ONLINE, onlineNum);
			// 登入时间
			Date logintime = log.getLoginTime();
			Calendar cs = Calendar.getInstance();
			cs.setTime(logintime);
			// 活动时间
			long active = (new Date()).getTime() - cs.getTimeInMillis();
			active = active / 1000;
			if (active > 1800) {
				active = active - 1800;
			}
			log.setActiveTime(active + "");
			String activeDesc = new StringBuffer().append(active / 3600).append(
					" 小时 ").append((active % 3600) / 60).append(" 分　").append(
					(active % 3600) % 60).append(" 秒").toString();
			log.setActiveTimeDesc(activeDesc);
			log.setLogoutTime(destoyedTime);
			//保存仿问日志
			
			try {
				service.create(log);
			} catch (Exception e) {
				AntiaLog.error("保存仿问日志时出现异常!" + e.getMessage());
				AntiaLog.error(e.getLocalizedMessage());
				usermap.remove(sessionId);
				e.printStackTrace();
			}
			// 从在线仿问用户列表中删除
			usermap.remove(sessionId);
		}
	}

}
